﻿// ICH KOMMENTIERE KEINEN CODE;
// ER WAR HART ZU SCHREIBEN
// ALSO SOLL ER HART ZU LESEN SEIN.

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace ecomerci
{
    public partial class newTransactions : Form
    {
        sqlite sql;
        applicationLogic logic;
        string customer_id;

        public customer customer;
        public transaction transaction;

        public newTransactions()
        {
            customer_id = "";
            logic = new applicationLogic();
            sql = new sqlite();
            
            InitializeComponent();
            txSelected_date.Text = dateDate.SelectionStart.ToString("dd.MM.yyyy");
            cbShipping.SelectedIndex = 0;
        }

        private void btSelect_customer_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = new DialogResult();
                selectCustomer selectCustomerForm = new selectCustomer();
                dr = selectCustomerForm.ShowDialog();

                switch (dr)
                {
                    case DialogResult.OK:
                        customer_id = selectCustomerForm.select_customer_id;
                        txEbay_username.Text = selectCustomerForm.select_ebay_name;
                        txName.Text = selectCustomerForm.select_first_name + " " + selectCustomerForm.select_last_name;
                        txStreet.Text = selectCustomerForm.select_street;
                        txHouse_number.Text = selectCustomerForm.select_house_number;
                        txArea_code.Text = selectCustomerForm.select_area_code;
                        txCity.Text = selectCustomerForm.select_city;
                        txEmail.Text = selectCustomerForm.select_email;
                        toggleCustomerForm(true);
                        break;

                    case DialogResult.Cancel:
                        selectCustomerForm.Close();
                        toggleCustomerForm(false);
                        break;
                }
            }
            catch (Exception ex)
            {
                logic.showException(ex);
            }
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            if(txOffer_title.Text != string.Empty)
            {
                customer = new customer(txName.Text, txStreet.Text + " " + txHouse_number.Text, txArea_code.Text, txCity.Text, txEmail.Text, txEbay_username.Text);
                transaction = new transaction(txOffer_number.Text, txOffer_title.Text, logic.getCustomerId(txEbay_username.Text.ToString()).ToString(), txPrice.Text, txPurchase_price.Text, cbShipping.SelectedIndex.ToString(), dateDate.SelectionStart.ToString("yyyyMMdd"));
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void dateDate_DateChanged(object sender, DateRangeEventArgs e)
        {
            txSelected_date.Text = dateDate.SelectionStart.ToString("dd.MM.yyyy");
        }

        public void fillForm(transaction Transaction, customer Customer)
        {
            txOffer_number.Enabled = false;
            txOffer_number.Text = Transaction.offer_number.ToString();
            txOffer_title.Text = Transaction.offer_title;
            txPrice.Text = Transaction.price.ToString();
            txPurchase_price.Text = Transaction.purchase_price.ToString();
            cbShipping.SelectedIndex = (int)Transaction.shipping;
            txSelected_date.Text = DateTime.Parse(Transaction.date.ToString()).ToString("dd.MM.yyyy");
            txName.Text = Customer.first_name + " " + Customer.last_name;
            txEbay_username.Enabled = false;
            txEbay_username.Text = Customer.ebay_username;
            string[] adress = Customer.adress.Split(' ');
            txStreet.Text = adress[0];
            txHouse_number.Text = adress[1].ToString();
            txArea_code.Text = Customer.area_code.ToString();
            txCity.Text = Customer.city;
            txEmail.Text = Customer.email;
            btSelect_customer.Enabled = true;
        }

        private void toggleCustomerForm(bool Disable)
        {
            if (Disable == true)
            {
                txEbay_username.Enabled = false;
                txName.Enabled = false;
                txStreet.Enabled = false;
                txHouse_number.Enabled = false;
                txArea_code.Enabled = false;
                txCity.Enabled = false;
                txEmail.Enabled = false;
            }
            else
            {
                txEbay_username.Enabled = true;
                txName.Enabled = true;
                txStreet.Enabled = true;
                txHouse_number.Enabled = true;
                txArea_code.Enabled = true;
                txCity.Enabled = true;
                txEmail.Enabled = true;
            }
        }

        private void txOffer_number_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txOffer_number, lbOffer_number, e);
        }

        private void txOffer_title_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txOffer_title, lbOffer_title, e);
        }

        private void txPrice_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txPrice, lbPrice, e);
        }

        private void txEbay_username_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txEbay_username, lbEbay_username, e);
        }

        private void txName_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txName, lbName, e);
        }

        private void txStreet_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txStreet, lbAdress, e);
        }

        private void txHouse_number_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txHouse_number, lbAdress, e);
        }

        private void txArea_code_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txArea_code, lbArea_code_city, e);
        }

        private void txCity_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txCity, lbArea_code_city, e);
        }

        private void txEmail_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txEmail, lbEmail, e);
        }

        private bool isEmail(string text)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        }

        private void addTransactions_Load(object sender, EventArgs e)
        {
            txOffer_number.Select();
        }

        private void txPurchase_price_Validating(object sender, CancelEventArgs e)
        {
            logic.validate(txPurchase_price, lbPurchase_price, e);
        }
    }
}
